package com.intellagric.service.traceability.Impl.trace_invest;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceInvestTypeMapper;
import com.intellagric.pojo.TraceFieldType;
import com.intellagric.pojo.TraceInvestType;
import com.intellagric.pojo.TraceInvestTypeExample;
import com.intellagric.service.traceability.trace_invest.InvestTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wl
 * @email: 844406720@qq.com
 * @Date: 2018/11/06
 * @Desc: 投入品类别管理服务实现
 */
@Service
public class InvestTypeServiceImpl implements InvestTypeService {

    @Autowired
    private TraceInvestTypeMapper traceInvestTypeMapper;

    /**
     * 添加投入品类别信息
     *
     * @param traceInvestType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addInvestType(TraceInvestType traceInvestType) {
        if (traceInvestTypeMapper.insertSelective(traceInvestType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除投入品类别信息
     *
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteInvestType(String id) {
        if (traceInvestTypeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改投入品类别信息
     *
     * @param traceInvestType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editInvestType(TraceInvestType traceInvestType) {
        if (traceInvestTypeMapper.updateByPrimaryKeySelective(traceInvestType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询投入品类别详细信息
     *
     * @param id
     * @return TraceInvestType
     */
    @Override
    public TraceInvestType getInvestTypeById(String id) {
        return traceInvestTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有品类分类信息列表
     *
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestTypeList() {
        TraceInvestTypeExample traceInvestTypeExample = new TraceInvestTypeExample();
        traceInvestTypeExample.setOrderByClause("sort");//升序排列
        traceInvestTypeExample.createCriteria().andIdIsNotNull();
        List<TraceInvestType> traceInvestTypeList = traceInvestTypeMapper.selectByExample(traceInvestTypeExample);
        return LayuiDataGridResult.success().add(traceInvestTypeList, traceInvestTypeList.size());
    }

    /**
     * 通过parentId查询品类分类列表，实现异步查询
     *
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestTypeByPid(String parentId) {
        TraceInvestTypeExample traceInvestTypeExample = new TraceInvestTypeExample();
        traceInvestTypeExample.setOrderByClause("sort");//升序排列
        traceInvestTypeExample.createCriteria().andParentIdEqualTo(parentId);
        List<TraceInvestType> traceInvestTypeList = traceInvestTypeMapper.selectByExample(traceInvestTypeExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(traceInvestTypeList);
        return result;
    }

    /**
     * 查询分类信息，并生成树形数据
     *
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestTypeTree() {
        List<TraceInvestType> investTypeList = this.getInvestTypeList().getData();
        List<TraceInvestType> rootInvestTypeList = this.getInvestTypeByPid("0").getData();

        for (TraceInvestType investType : rootInvestTypeList) {
            investType.setChildren(getChild(investType.getId(), investTypeList));
        }

        return LayuiDataGridResult.success().add(rootInvestTypeList,rootInvestTypeList.size());
    }
    /**
     * 查询分类信息，并生成树形数据
     *
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestTypeTree(String createBy) {
        TraceInvestTypeExample example=new TraceInvestTypeExample();
        example.createCriteria().andCreateByEqualTo(createBy);
        List<TraceInvestType> investTypeList = traceInvestTypeMapper.selectByExample(example);
        example.createCriteria().andCreateByEqualTo(createBy).andParentIdEqualTo("0");
        List<TraceInvestType> rootInvestTypeList =traceInvestTypeMapper.selectByExample(example);
        for (TraceInvestType investType : rootInvestTypeList) {
            investType.setChildren(getChild(investType.getId(), investTypeList));
        }

        return LayuiDataGridResult.success().add(rootInvestTypeList,rootInvestTypeList.size());
    }


    /**
     * 递归查询子分类列表
     *
     * @param id
     * @param investTypeList
     * @return List<TraceInvestType>
     */
    private List<TraceInvestType> getChild(String id, List<TraceInvestType> investTypeList) {
        // 子菜单
        List<TraceInvestType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (TraceInvestType investType : investTypeList) {
            if (investType.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (investType.getParentId().equals(id)) {
                    childList.add(investType);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (TraceInvestType investType : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            investType.setChildren(getChild(investType.getId(), investTypeList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}